package com.example.redis.bit.controller;

import com.example.redis.bit.ActInstanceFactory;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RedisBitController {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RedisConnection redisConnection;

    //这个接口用于模拟一亿用户5天的登陆活跃度，后面的实现用lua实现。
    //这个接口为异步，redis初始化模拟数据需要等一段时间，可以刷新redis可视化界面进行查看
    //例子：http://127.0.0.1:8200/act/init
    @GetMapping("/act/init")
    @ResponseBody
    public String initData(){
        ActInstanceFactory.getInstance().initData();
        return "Initializating Data";
    }

    //某一天的用户活跃数
    //例子：http://127.0.0.1:8200/act/20190101
    @GetMapping("/act/{date}")
    @ResponseBody
    public long countUsers(@PathVariable String date){
        return ActInstanceFactory.getInstance().countUsers(date);
    }

    //某一天的redis内存占用
    //例子：http://127.0.0.1:8200/mem/20190101
    @GetMapping("/mem/{date}")
    @ResponseBody
    public String mem(@PathVariable String date){
        return ActInstanceFactory.getInstance().mem(date)/1024 + "K";
    }

    //某一天的某一个用户是否活跃
    //例子：http://127.0.0.1:8200/act/user/20190101/10020
    @GetMapping("/act/user/{date}/{id}")
    @ResponseBody
    public boolean user(@PathVariable String date, @PathVariable Long id){
        return ActInstanceFactory.getInstance().user(date,id);
    }

    //任意时间段内某一个用户是否活跃
    //例子：http://127.0.0.1:8200/act/user/20190101/20190102/10020
    @GetMapping("/act/user/{dateBegin}/{dateEnd}/{id}")
    @ResponseBody
    public boolean user(@PathVariable String dateBegin, @PathVariable String dateEnd, @PathVariable Long id){
        return ActInstanceFactory.getInstance().user(dateBegin, dateEnd, id);
    }

    //任意时间段内用户活跃数
    //例子：http://127.0.0.1:8200/act/20190101/20190102
    @GetMapping("/act/{dateBegin}/{dateEnd}")
    @ResponseBody
    public long act(@PathVariable String dateBegin, @PathVariable String dateEnd){
        return ActInstanceFactory.getInstance().act(dateBegin, dateEnd);
    }
}
